package com.shopping.servlet.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.shopping.pojo.Order;
import com.shopping.service.OrderService;
import com.shopping.service.impl.OrderServiceImpl;

@WebServlet("/UpdateOrderServlet")
public class UpdateOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public UpdateOrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		long id = Long.parseLong(request.getParameter("o_id"));
		String name = request.getParameter("o_name");
		String phone = request.getParameter("o_phone");
		String content = request.getParameter("o_content");
		Double money = Double.parseDouble(request.getParameter("o_money"));
		int status = Integer.valueOf(request.getParameter("o_status"));
		int pay = Integer.valueOf(request.getParameter("o_pay_method"));
		String o_time = request.getParameter("o_time");
		String dec = request.getParameter("o_description");
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time = new Date();
		try {
			time = sdf.parse(o_time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Order order = new Order(name,phone,content,money,status,pay,time,dec);
		order.setO_id(id);
		
		System.out.println(order);
		OrderService os = new OrderServiceImpl();
		JSONObject jo = new JSONObject();
		
		
		boolean b = os.updateOrder(order);
		if(b==true) {
			jo.put("code", 0);
			jo.put("msg", order.getO_id());
		}
		else {
			jo.put("code", 1);
			jo.put("msg", order.getO_id());
		}
		jo.put("count", 1);
		System.out.println(jo);
		out.print(jo);
	}

}
